#include "filethread.h"

#include "backendthread.h"
#include "database.h"


FileThread::FileThread(int socketDescriptor)
{
    connection=new QTcpSocket(this);

    if(connection->setSocketDescriptor(socketDescriptor)) {
        connect(connection, SIGNAL(readyRead()),
                     this, SLOT(read()));
        qDebug()<<"opening connection";
    } else {
        qDebug()<<"opening connection failed";
    }
    connect(connection,SIGNAL(disconnected()),this,SLOT(deleteLater()));
}

void FileThread::run() {
    if(!connection->isOpen()) {
        connection->open(QIODevice::ReadWrite);
    } else {
        qDebug()<<"connection is open";
    }
}

void FileThread::read() {
    qDebug()<<"reading";

    QString result(connection->readAll());
    QStringList list=result.split(QRegExp("( |\n)"));

    if (list.at(0)=="GET") {
        QString mediaID=list.at(1);
        int index=mediaID.lastIndexOf(".")-1;
        if(index<0) {
            index=-1;
        }
        mediaID=mediaID.mid(1,index);
        qDebug()<<"media id is "<<mediaID;
        QList<MediaFile *> * files=Querier::getFilesOfType("","mediaID='"+mediaID+"'");
        qDebug()<<files;
        if(files&&files->size()>0) {
            MediaFile * mediaFile= files->at(0);
            
            response.clear();
            qDebug()<<mediaFile;
            QFile file((mediaFile->fileLocation+"/"+mediaFile->shortName));
            file.open(QIODevice::ReadOnly);
            qDebug()<<"file exists? "<<file.exists();
            QFile file2("test.txt");
            file2.open(QIODevice::WriteOnly);
            QTextStream out(connection);
            out.setAutoDetectUnicode(true);;
            out<<"HTTP/1.1 200 Ok\r\n";
            //out<<"content-type: application/octet-stream\r\n";
            out<<"Content-Type: video/mp4\r\n";
            QString name=mediaFile->shortName.replace(" ","_");
            out<<"Content-Disposition: disp-extension-token; filename="<<name<<";\r\n";
            out<<"Last-Modified: Tue, 1 Jan 2010 05:00:01 GMT\r\n";
            //out<<"Accept-Ranges: bytes\r\n";
            //out<<("Etag: \"media"+mediaID+"\"\r\n");
            out<<"Date: Tue, 1 Jun 2010 03:00:00 GMT\r\n";
            //out<<"Content-Encoding: token\r\n";
            //out<<("Content-Transfer-Encoding: base64\r\n");
            out<<("Content-Length: "+QString::number(file.size())+"\r\n");//*/

//            response.append("RESPONSE: HTTP/1.1 200 Ok\n");
//            response.append("Content-Disposition: attachment\n");
//            response.append("Content-Type: application/octet-steam\n");
//            response.append("Accept-Ranges: bytes\r\n");
//            response.append("Content-Length: "+QString::number(file.size())+"\r\n");
            out<<"\r\n";
            out.flush();
            char * character=new char [1];
            /*while(file.getChar(character)) {
                connection->write(character);
            }*/
            connection->write(file.readAll());
            //file.seek(0);
            /*while(file.getChar(character)) {
                file2.write(character);
            }*/
            //file2.write(file.readAll());
            //file2.close();
           // out<<file.readAll();

            //qDebug()<<connection->readAll();
            //FILE * f=fopen(file.fileName(),"r");

            //connection->write(response);
            //response.clear();
            //connection->close();
            //out << (quint64)0; //placeholder for size do not forget
        }


    } else if(list.at(0)=="RETR") {

    }


}

void FileThread::write() {

}
